﻿using DevExpress.XtraEditors;
using System;
using System.Net.Http;
using System.Text;
using System.Windows.Forms;
using Newtonsoft.Json;

namespace BW.Mes.WinForms.BasicData
{
    public partial class AddCustomerForm : DevExpress.XtraEditors.XtraForm
    {
        private readonly HttpClient _httpClient = new HttpClient();
        private readonly string _apiBaseUrl = "https://localhost:7274";
        public AddCustomerForm()
        {
            InitializeComponent();
        }
        private async void btnOK_Click(object sender, EventArgs e)
        {
            var model = new
            {
                CustomerCode = textEditCode.Text,
                CustomerName = textEditName.Text,
                Abbreviation = textEditAbbreviation.Text,
                Phone = textEditPhone.Text,
                Address = textEditAddress.Text,
                USCC = textEditUSCC.Text,
                CustomerMsg = textEditMsg.Text,
                CreatedBy = "wyj"
            };
            string jsonData = JsonConvert.SerializeObject(model);
            var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
            var response = await _httpClient.PostAsync($"{_apiBaseUrl}/api/Customer/AddCustomer", content);
            if (response.IsSuccessStatusCode)
            {
                XtraMessageBox.Show("新增成功！");
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                var errorMsg = await response.Content.ReadAsStringAsync();
                XtraMessageBox.Show($"新增失败！\n状态码: {response.StatusCode}\n内容: {errorMsg}");
            }
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}